package cn.melon.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JLabel;

import cn.melon.helper.BufferedImageHelper;

/**
 * @ClassName: JDataLabel
 * @author Hzy
 * 用于展示 X门待上课程和X次早起勤奋跑操的按钮，主要在于背景透明/数值改变
 */
public class JDataLabel extends JLabel{
    private int width,height;
    private String data;//数据
    private String unit;//单位
    private String name;//名称
    private Font dataFont;//数据的字体
    private Font wordFont;//文字的字体
    private Color color;//文字的颜色
    private String bacPath;//上级容器的背景图片
    private Point location;
    private JFrame root;
    public JDataLabel(int width,int height,String data,String unit,String name,Font dataFont,Font wordFont,Color color,String bacPath,JFrame window){
        super();
        this.width = width;
        this.height = height;
        this.data = data;
        this.unit = unit;
        this.name = name;
        this.dataFont = dataFont;
        this.wordFont = wordFont;
        this.location = new Point();
        this.color = color;
        this.bacPath = bacPath;
        this.root = window;
        this.setPreferredSize(new Dimension(width,height));
        this.setBorder(null);
    }
    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
//        清空上次绘制的内容
        g2d.clearRect(0, 0, width, height);
//        绘制该JLabel的背景图像
        BufferedImage bacImg = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
        try{
//            bacImg = ImageIO.read(new File(bacPath));
        }catch(Exception e){
            e.printStackTrace();
        }
        bacImg = BufferedImageHelper.getSource(this.getClass(), bacPath);

        this.location = new Point((this.getLocationOnScreen().x - root.getLocationOnScreen().x),(this.getLocationOnScreen().y-root.getLocationOnScreen().y));
        bacImg = bacImg.getSubimage(location.x,location.y,width,height);//绘制当前按钮背景为父容器相应位置的背景
        g2d.drawImage(bacImg, 0, 0, null);
        g2d.setColor(new Color(0f,0f,0f,0.23f));//黑色半透明遮罩
        g2d.fillRect(0, 0, width, height);
//        获取各个文本字符串的高度和宽度
        FontMetrics metrics_data = g2d.getFontMetrics(dataFont);
        int dataWidth = metrics_data.stringWidth(data);
        int dataHeight = metrics_data.getHeight();
        FontMetrics metrics_word = g2d.getFontMetrics(wordFont);
        int unitWidth = metrics_word.stringWidth(unit);
        int unitHeight = metrics_word.getHeight();
        int nameWidth = metrics_word.stringWidth(name);
        int nameHeight = unitHeight;
//        设置单位和数据名称之间的行距
        int lineGap = 1;
//        计算各文本的绘制位置
        int x_data = (int) (width*0.47 - dataWidth);
        int y_data = (height - dataHeight)/2 + metrics_data.getAscent();
        int x_name = (int) (width*0.50);
        int y_name = y_data - 2;
        int x_unit = x_name;
        int y_unit = y_name - lineGap - unitHeight;
//        绘制文本
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//开启抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);//渲染质量高
        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);//关闭抖动
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);//画笔控制
        g2d.setColor(color);
        g2d.setFont(dataFont);
        g2d.drawString(data, x_data, y_data);
        g2d.setFont(wordFont);
        g2d.drawString(unit, x_unit, y_unit);
        g2d.drawString(name, x_name, y_name);
    }
    
    public void setData (String data){
//        修改数据
        this.data = data;
        this.repaint();
    }
}
